package cn.kgc.controller;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.kgc.entity.Emp;
import cn.kgc.service.EmpService;

@Controller
public class EmpController {
	
	@Autowired
	private EmpService empService;
	
	@RequestMapping(path="/emp",method = RequestMethod.GET)
	//@ResponseBody
	public String getAllEmp(Model model){
		List<Emp> list=empService.getAllEmp();
		model.addAttribute("empList",list);
		return "empList";
	}
	
	/*
	@RequestMapping("/emp2")
	@ResponseBody
	public List<Emp> getAllEmp2(){
		List<Emp> list=empService.getAllEmp();
		System.out.println(list);
		return list;
	}*/
	
	@RequestMapping(path="/emp/add",method = RequestMethod.GET)
	public String toEmpUI(Model model) {
		model.addAttribute("emp", new Emp());
		return "emp";
	}
	
	@RequestMapping(path = "/emp",method = RequestMethod.POST)
	public String addEmp(Emp emp) {
		empService.addEmp(emp);
		return "redirect:/emp";
	}
	
	@RequestMapping(path = "/emp/{id}",method = RequestMethod.GET)
	public String getEmpById(@PathVariable("id") Integer id,Model model) {
		Emp emp=empService.getEmpById(id);
		model.addAttribute("emp", emp);
		return "emp";
	}
	
	@RequestMapping(path = "/emp",method = RequestMethod.PUT)
	public String updateEmp(Emp emp) {
		empService.updateEmp(emp);
		return "redirect:/emp";
	}
	
	@RequestMapping(path = "/emp/{id}",method = RequestMethod.DELETE)
	public String deleteEmpById(@PathVariable("id") Integer id) {
		empService.deleteEmpById(id);
		return "redirect:/emp";
	}
	
	
}
